<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('国际化词条') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('编码')" prop="code">
        <el-input
          v-model="queryParams.code"
          :placeholder="$t('编码')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('中文')" prop="zhCn">
        <el-input
          v-model="queryParams.zhCn"
          :placeholder="$t('中文')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('英文')" prop="enUs">
        <el-input
          v-model="queryParams.enUs"
          :placeholder="$t('英文')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['dev:language:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['dev:language:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-col>
    </el-row>
    <el-form ref="form" :model="form" :rules="rules" class="dataForm">
      <el-table v-loading="loading" :data="form.sysLanguageList" border class="nowrapTable formTable">
        <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
          <template slot-scope="scope">
            {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('编码')" align="center" prop="code" />
        <el-table-column align="center">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('中文') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'sysLanguageList.' + scope.$index + '.zhCn'" :rules="rules.zhCn">
              <el-input
                v-model="scope.row.zhCn"
                :placeholder="$t('中文')"
                clearable
                @focus="obtainVal(scope.row)"
                @blur="saveForm(scope.row)"
              />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column align="center">
          <template slot="header" slot-scope="{}">
            <!-- <i style="color: #ff4949;margin-right: 4px;">*</i> -->
            <span>{{ $t('英文') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'sysLanguageList.' + scope.$index + '.enUs'" :rules="rules.enUs">
              <el-input
                v-model="scope.row.enUs"
                :placeholder="$t('英文')"
                clearable
                @focus="obtainVal(scope.row)"
                @blur="saveForm(scope.row)"
              />
            </el-form-item>
          </template>
        </el-table-column>

        <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
          <template slot-scope="scope">
            <el-button
              v-hasPermi="['dev:language:save']"
              size="small"
              type="text"
              icon="el-icon-edit"
              @click="updateHandle(scope.row)"
            >{{ $t('修改') }}</el-button>
            <el-button
              v-hasPermi="['dev:language:remove']"
              size="small"
              type="text"
              icon="el-icon-delete"
              @click="deleteHandle(scope.row)"
            >{{ $t('删除') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改国际化词条对话框 -->
    <el-dialog :title="$t('国际化词条')" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <sysLanguageForm ref="sysLanguageForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listSysLanguage, delSysLanguage, exportSysLanguage } from '@/api/dev/language/sysLanguage'
import { saveSysLanguage } from '@/api/dev/language/sysLanguage'
import sysLanguageForm from './sysLanguageForm.vue'
export default {
  components: {
      sysLanguageForm
  },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 是否显示弹出层
          formOpen: false,
          // 查询参数
          queryParams: {
              code: undefined,
              zhCn: undefined,
              enUs: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {
            // 国际化词条表格数据
            sysLanguageList: [],
            data: {}
          },
          // 表单校验
          rules: {
            zhCn: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
            // enUs: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
          }
      }
  },
  created() {
      this.getList()
  },
  methods: {
    obtainVal(val) {
      this.form.data = val
    },
    saveForm() {
      this.loading = true
      this.$refs['form'].validate(valid => {
        if (valid) {
          saveSysLanguage(this.form.data).then(response => {
              if (response.code === 200) {
                  this.msgSuccess(this.$t('保存成功'))
                  this.getList()
              } else {
                  this.msgError(response.msg)
              }
          })

          if (this.$i18n.vm.locale === 'zh_cn') {
            this.$i18n.vm.messages.zh_cn[this.form.data.code] = this.form.data.zhCn
          } else if (this.$i18n.vm.locale === 'en_us') {
            this.$i18n.vm.messages.en_us[this.form.data.code] = this.form.data.enUs
          }
          // console.log(this.$i18n.vm.messages)
          this.$nextTick(() => {
            this.updateLocalStorage()
            // this.$refs.sysLanguageForm.updateLocalStorage()
          })
        }
      })
    },
    updateLocalStorage() {
      var zhCn = window.localStorage.getItem('language_zh_cn')
      var enUs = window.localStorage.getItem('language_en_us')

      var zjCnJson = (zhCn !== undefined && zhCn !== null) ? JSON.parse(zhCn) : {}
      var enUsJson = (enUs !== undefined && enUs !== null) ? JSON.parse(enUs) : {}
      zjCnJson[this.form.data.code] = this.form.data.zhCn
      enUsJson[this.form.data.code] = this.form.data.enUs

      window.localStorage.setItem('language_zh_cn', JSON.stringify(zjCnJson))
      window.localStorage.setItem('language_en_us', JSON.stringify(enUsJson))
    },
    /** 查询国际化词条列表 */
    getList() {
        this.loading = true
        this.form.sysLanguageList = []
        listSysLanguage(this.queryParams).then(response => {
            this.form.sysLanguageList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.sysLanguageForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.sysLanguageForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delSysLanguage(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return exportSysLanguage(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(function() {
        })
    },
    editable(row) {
      row.isEdit = true
    }
  }
}
</script>
